package com.hibernate.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.Session;

import com.hibernate.db.model.Label;
import com.hibernate.db.model.Language;
import com.hibernate.db.model.Vocabulary;
import com.hibernate.facade.VocabularyFacade;
import com.hibernate.util.SessionFactoryUtil;

public class VocabularyService {

	int vocID;
	int sourceLangID;
	int targetLangID;

	VocabularyFacade vocFacade;
	String sourceLang;
	String targetLang;
	Session session;

	public VocabularyService() {
		session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
	}

	public VocabularyFacade getVocabulary(int vocID, int sourceLangID,
			int targetLangID) {
		session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		vocFacade = new VocabularyFacade();
		this.vocID = vocID;
		this.sourceLangID = sourceLangID;
		this.targetLangID = targetLangID;
		if (!session.getTransaction().isActive()) {
			SessionFactoryUtil.beginnTransaction();
		}
		
		vocFacade.setSource(this.getLabel(vocID, sourceLangID));
		vocFacade.setTarget(this.getLabel(vocID, targetLangID));
		vocFacade.setVocId(vocID);
		vocFacade.setPicPath(this.getVocabularyPic(vocID));
		return vocFacade;
	}

	public List<VocabularyFacade> getAllVocabularies(int sourceLang,
			int targetLang) {
		session = SessionFactoryUtil.getSessionFactory().getCurrentSession();
		SessionFactoryUtil.beginnTransaction();
		
		this.sourceLang = (((Language)(session.byId(Language.class).load(sourceLang))).getLang());
		this.targetLang = (((Language)(session.byId(Language.class).load(targetLang))).getLang());
		System.out.println(this.sourceLang);
		
		List<VocabularyFacade> vocfacades = new ArrayList<VocabularyFacade>();
		
		List<Vocabulary> results = session.createQuery("from Vocabulary").list();
		for (Vocabulary vocabulary : results) {
			vocfacades.add(this.getVocabulary(vocabulary.getId(), sourceLang,
					targetLang));

		}
		SessionFactoryUtil.commitTransaction();
		return vocfacades;
	}

	private String getLabel(int vocID, int langID) {
		Query query = session.getNamedQuery("getLabelForVocabularyByLang");
		query.setString("vocID", Integer.toString(vocID));
		query.setString("langID", Integer.toString(langID));
		List<Label> stList = query.list();
		Iterator<Label> stIterator = stList.iterator();
		Object obj = null;
		Label label = null;
		while (stIterator.hasNext()) {
			obj = stIterator.next();
			label = (Label) obj;
		}
		return label.getWord();
	}

	private String getVocabularyPic(int vocId) {
		Object obj = session.byId(Vocabulary.class).load(vocId);
		Vocabulary voc = (Vocabulary) obj;
		return voc.getPicPath();
	}

	public String getSourceLang() {
		return sourceLang;
	}

	public void setSourceLang(String sourceLang) {
		this.sourceLang = sourceLang;
	}

	public String getTargetLang() {
		return targetLang;
	}

	public void setTargetLang(String targetLang) {
		this.targetLang = targetLang;
	}
}
